package com.diancan.web.controller.dc;

import java.util.Date;
import java.util.List;

import com.diancan.common.core.text.Convert;
import com.diancan.common.utils.ShiroUtils;
import com.diancan.common.utils.uuid.IdUtils;
import com.diancan.dc.domain.DcFoodInfo;
import com.diancan.dc.domain.DcOrderInfo;
import com.diancan.dc.service.IDcFoodInfoService;
import com.diancan.dc.service.IDcOrderInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.diancan.common.annotation.Log;
import com.diancan.common.enums.BusinessType;
import com.diancan.dc.domain.OrderDetail;
import com.diancan.dc.service.IOrderDetailService;
import com.diancan.common.core.controller.BaseController;
import com.diancan.common.core.domain.AjaxResult;
import com.diancan.common.utils.poi.ExcelUtil;
import com.diancan.common.core.page.TableDataInfo;

/**
 * 订单详情Controller
 *
 * @author dc
 * @date 2021-12-27
 */
@Controller
@RequestMapping("/dc/orderDetail")
public class OrderDetailController extends BaseController {
    private String prefix = "dc/orderDetail";

    @Autowired
    private IOrderDetailService orderDetailService;

    @Autowired
    private IDcFoodInfoService dcFoodInfoService;
    @Autowired
    private IDcOrderInfoService dcOrderInfoService;

    @RequiresPermissions("dc:orderDetail:view")
    @GetMapping()
    public String orderDetail() {
        return prefix + "/orderDetail";
    }

    /**
     * 查询订单详情列表
     */
    @RequiresPermissions("dc:orderDetail:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDetail orderDetail) {
        orderDetail.setUserName(ShiroUtils.getLoginName());
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        return getDataTable(list);
    }

    @PostMapping("/listByOederId")
    @ResponseBody
    public TableDataInfo listByOederId(OrderDetail orderDetail) {
        startPage();
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        return getDataTable(list);
    }

    /**
     * 导出订单详情列表
     */
    @RequiresPermissions("dc:orderDetail:export")
    @Log(title = "订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDetail orderDetail) {
        List<OrderDetail> list = orderDetailService.selectOrderDetailList(orderDetail);
        ExcelUtil<OrderDetail> util = new ExcelUtil<OrderDetail>(OrderDetail.class);
        return util.exportExcel(list, "订单详情数据");
    }

    /**
     * 新增订单详情
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单详情
     */
    @RequiresPermissions("dc:orderDetail:add")
    @Log(title = "订单详情", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrderDetail orderDetail) {
        DcFoodInfo dcFoodInfo = dcFoodInfoService.selectDcFoodInfoById(orderDetail.getFoodId());
        orderDetail.setCarId(IdUtils.simpleUUID());
        orderDetail.setUserName(ShiroUtils.getLoginName());
        orderDetail.setFoodName(dcFoodInfo.getFoodName());
        orderDetail.setFoodPrice(dcFoodInfo.getFoodPrice());
        return toAjax(orderDetailService.insertOrderDetail(orderDetail));
    }


    /**
     * 修改订单详情
     */
    @RequiresPermissions("dc:orderDetail:edit")
    @GetMapping("/edit/{carId}")
    public String edit(@PathVariable("carId") String carId, ModelMap mmap) {
        OrderDetail orderDetail = orderDetailService.selectOrderDetailByCarId(carId);
        mmap.put("orderDetail", orderDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单详情
     */
    @RequiresPermissions("dc:orderDetail:edit")
    @Log(title = "订单详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderDetail orderDetail) {
        return toAjax(orderDetailService.updateOrderDetail(orderDetail));
    }

    /**
     * 删除订单详情
     */
    @RequiresPermissions("dc:orderDetail:remove")
    @Log(title = "订单详情", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(orderDetailService.deleteOrderDetailByCarIds(ids));
    }

    @RequestMapping("/jieSuan")
    public String jieSuan(String ids, ModelMap mmap) {
        String[] strings = Convert.toStrArray(ids);
        String uuid = IdUtils.simpleUUID();
        Integer totalPrice = 0;
        for (String string : strings) {
            OrderDetail detail = orderDetailService.selectOrderDetailByCarId(string);
            String foodPrice = detail.getFoodPrice();
            String foodNumber = detail.getFoodNumber();
            totalPrice += Integer.valueOf(foodPrice.trim()) * Integer.valueOf(foodNumber.trim());
            detail.setOrderId(uuid);
            orderDetailService.updateOrderDetail(detail);
        }

        DcOrderInfo orderInfo = new DcOrderInfo();
        orderInfo.setId(uuid);
        orderInfo.setUserName(ShiroUtils.getLoginName());
        orderInfo.setCreateTime(new Date());
        orderInfo.setTotalPrice(String.valueOf(totalPrice));
        orderInfo.setStatus("2");
        dcOrderInfoService.insertDcOrderInfo(orderInfo);
        mmap.put("dcOrderInfo", orderInfo);
        return prefix + "/jieSuan";
    }

}
